package com.ycsyxt.guide;

/**
 * @author ycs(15241496739 @ 163.com)
 * @date 2022/6/5
 **/
public class Class3 {
    // 迭代法，不断使用旧的变量值，递归计算新的变量
    public static long getNumWithWheat(int grid) {
        // return (1 << grid) - 1;
        long sum = 1;
        long init = 1;
        for (int i = 2; i <= grid; i++) {
            init *= 2;
            sum += init;
        }
        return sum;
    }
    // 二分法，在有序集合查找目标
    public static int searchIndex(int[] nums, int target) {
        int left = 0;
        int right = nums.length - 1;

        while (left <= right) {
            int mid = left + (right - left) / 2;
            if (nums[mid] == target) {
                return mid;
            } else if (nums[mid] < target) {
                left = mid + 1;
            } else {
                right = mid - 1;
            }
        }
        return -1;
    }
    public static void main(String[] args) {
        int[] nums = new int[]{1,3,5,7,9};
        System.out.println(searchIndex(nums, 3));
    }
}
